package avicit.bdp.dcs.alert.rest;

import avicit.bdp.dcs.alert.dto.AlertDTO;
import avicit.bdp.dcs.alert.service.AlertService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2020-06-29 11:55
 * @类说明：告警配置接口
 * @修改记录：
 */
@RestController
@RequestMapping("/api/bdp/dcs/alert/AlertRest")
@Api(tags = "告警配置接口")
public class AlertRest {

    private static final Logger logger = LoggerFactory.getLogger(AlertRest.class);

    @Autowired
    private AlertService alertService;

    @GetMapping("/getAlertById")
    @ApiOperation(value = "按主键查询数据")
    public ResponseMsg<AlertDTO> getAlertById(@ApiParam(value = "主键id", name = "id", required = true) @RequestParam String id) {
        ResponseMsg<AlertDTO> responseMsg = new ResponseMsg<>();
        try {
            responseMsg.setResponseBody(alertService.getAlertById(id));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
            responseMsg.setErrorDesc(e.getMessage());
        }
        return responseMsg;
    }

    @GetMapping("/getAlertListByJobId")
    @ApiOperation(value = "根据jobid查询告警数据列表")
    public ResponseMsg<List<AlertDTO>> getAlertListByJobId(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) {
        ResponseMsg<List<AlertDTO>> responseMsg = new ResponseMsg<>();
        try {
            responseMsg.setResponseBody(alertService.getAlertListByJobId(jobId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
            responseMsg.setErrorDesc(e.getMessage());
        }
        return responseMsg;
    }

    @PostMapping("/updateAlert")
    @ApiOperation(value = "修改告警规则")
    public ResponseMsg updateAlert(@RequestBody AlertDTO alertDTO) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            alertService.updateAlert(alertDTO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
            responseMsg.setErrorDesc(e.getMessage());
        }
        return responseMsg;
    }

    @PostMapping("/updateAlertStatus")
    @ApiOperation(value = "启用禁用监控报警信息")
    public ResponseMsg updateAlertStatus(@RequestBody AlertDTO alertDTO) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            alertService.updateAlertStatus(alertDTO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
            responseMsg.setErrorDesc(e.getMessage());
        }
        return responseMsg;
    }
}
